package com.spa.infrastructure.service.wx;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class WxConfig {

    @Value("${wx.serialNo}")
    private String serialNo;

    @Value("${wx.privateKey}")
    private String privateKey;

    @Value("${wx.apiV3Key}")
    private String apiV3Key;

    @Value("${wx.mchId}")
    private String mchId;

    /**
     * 商家转账到零钱
     * @return
     */
    @Bean
    public TransferBatchService transferBatchService() {
        return new TransferBatchService.Builder()
                .config(rsaAutoCertificateConfig())
                .build();
    }

    /**
     * jsapi下单
     * @return
     */
    @Bean
    public JsapiServiceExtension jsapiServiceExtension() {
        return new JsapiServiceExtension.Builder()
                .config(rsaAutoCertificateConfig())
                .build();
    }

    /**
     * 退款
     * @return
     */
    @Bean
    public RefundService refundService() {
        return new RefundService.Builder()
                .config(rsaAutoCertificateConfig())
                .build();
    }

    @Bean
    public RSAAutoCertificateConfig rsaAutoCertificateConfig() {
        return new RSAAutoCertificateConfig.Builder()
                .apiV3Key(apiV3Key)
                .merchantId(mchId)
                .merchantSerialNumber(serialNo)
                .privateKey(privateKey)
                .build();
    }
}
